package main

import (
	"net/http"

	"github.com/gin-gonic/gin"
)

//各种响应式处理结果
func main() {
	r := gin.Default()

	r.GET("person/:name/:id", func(c *gin.Context) { //参数作为URL格式：Restful风格
		c.JSON(200, gin.H{
			"name": c.Param("name"),
			"id":   c.Param("id"),
		})
	})
	// 	curl "http://localhost:8888/person/fmx/26"
	// {"id":"26","name":"fmx"}

	r.GET("/user", func(c *gin.Context) {
		c.JSONP(http.StatusOK, gin.H{ //带有返回值
			"name":   "fmx",
			"sarlay": "15000",
			"age":    22,
		})
	})
	//curl "http://localhost:8888/user?callback=ab"
	//ab({"age":22,"name":"fmx","sarlay":"15000"});

	r.SecureJsonPrefix("{F}")
	r.GET("/user2", func(c *gin.Context) {
		arr := [...]string{"a", "b", "c"}
		c.SecureJSON(http.StatusOK, arr)
	})
	// curl "http://localhost:8888/user2"
	// {F}["a","b","c"]

	r.GET("/user3", func(c *gin.Context) {
		c.AsciiJSON(http.StatusOK, gin.H{
			"name":  "张三",
			"email": "zs@qq.com",
			"age":   20,
		})
	})
	// 	curl "http://localhost:8888/user3"
	// {"age":20,"email":"zs@qq.com","name":"\u5f20\u4e09"}

	r.GET("/user4", func(c *gin.Context) {
		c.YAML(http.StatusOK, gin.H{
			"name":  "orange",
			"email": "orange@qq.com",
		})
	})
	// curl "http://localhost:8888/user4"
	// email: orange@qq.com
	// name: orange

	// Serves literal characters
	r.GET("/purejson", func(c *gin.Context) {
		c.PureJSON(200, gin.H{
			"html": "<b>Hello, world!</b>",
		})
	})
	r.Run(":8888")
}
